package android07.newtop.presenter;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.jude.beam.bijection.Presenter;
import com.jude.beam.expansion.data.BeamDataFragmentPresenter;

import android07.newtop.adapter.WeatherListAdapter;
import android07.newtop.fragment.WeaFragment;
import android07.newtop.fragment.WeatherFragment;
import android07.newtop.model.WeatherBeanModel;
import android07.newtop.model.bean.WeatherInfoBean;
import android07.newtop.utils.JUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Subscriber;


/**
 * 天气Fragment的主控者
 */
public class WeaFragmentPresenter  extends BeamDataFragmentPresenter<WeaFragment,WeatherInfoBean> implements SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = WeaFragmentPresenter.class.getSimpleName();

    private String tab;
    @Override
    protected void onCreate(WeaFragment view, Bundle savedState) {
        super.onCreate(view, savedState);
        tab=view.getArguments().getString("tabvalue");
    }


    @Override
    protected void onCreateView(@NonNull WeaFragment view) {
        super.onCreateView(view);
        getDataByTab(tab);
    }

    private void getDataByTab(String tab) {
        new WeatherBeanModel().getWeatherInfoBeanList(tab, new Callback<WeatherInfoBean>() {
            @Override
            public void onResponse(Call<WeatherInfoBean> call, Response<WeatherInfoBean> response) {
                WeatherInfoBean weatherInfoBean = response.body();
                if(weatherInfoBean!=null&&weatherInfoBean.getError_code()==0){

                    getDataSubscriber().onNext(weatherInfoBean);
                    if( getView().swipeRefreshLayout.isRefreshing()){
                        getView().swipeRefreshLayout.setRefreshing(false);
                    }

                    Log.d(TAG, "onResponse: 请求数据完毕");
                }

            }

            @Override
            public void onFailure(Call<WeatherInfoBean> call, Throwable t) {
                Log.d(TAG, "onFailure: 网络错误"+t.toString());
                if( getView().swipeRefreshLayout.isRefreshing()){
                    getView().swipeRefreshLayout.setRefreshing(false);
                }

            }
        });
    }

    @Override
    public void onRefresh() {
        getDataByTab(tab);
    }
}
